home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol03 / 05 / msctsr / msctsr.c < prev    next >
C/C++ Source or Header  |  1988-08-31  |  5KB  |  164 lines

  1. /* Figure 4. MSCTSR.EXE - A TSR to pop up the time. */
  2.  
  3.    /*
  4.     * tsr to pop up the time
  5.     */
  6.  
  7.    /* Additional Notes:
  8.     * Note that MSCTSR.EXE can be loaded more than once and will not
  9.     * check to see if it is already loaded. A hot key combination
  10.     * will be intercepted by each instance of MSCTSR that is loaded.
  11.     * What results is that, for example, if three instances of MSCTSR
  12.     * are loaded, when CTRL-W is hit, each instance of MSCTSR will
  13.     * pop-up the time and restore the screen before control is returned.
  14.     */
  15.  
  16.    #include "tsrbios.h"
  17.    #include <dos.h>
  18.    #include <bios.h>
  19.    extern int _psp, end;
  20.    extern unsigned _osmajor;
  21.  
  22.    extern union REGS regs;
  23.  
  24.    #define HOTKEY_CODE     0x11      /* W key */
  25.    #define HOTKEY_STAT     0x4       /* ctrl */
  26.    #define KEYPORT         0x60      /* keyboard i/o port */
  27.  
  28.    #define DOS_INT         0x21
  29.    #define KEYSVC_INT      0x9
  30.    #define DOS_PRINTSTRING 0x9
  31.  
  32.    #define StartOfTime 6   /* 6 characters into msg string */
  33.    char msg[] = "****  HH:MM:SS  ****";
  34.    char buf[] = "                    ";
  35.  
  36.    /* variables for time calculations */
  37.    unsigned long ticks;
  38.    int h,m,s;
  39.    char *sptr;
  40.  
  41.    void (interrupt far * oldint9)();
  42.  
  43.    /*
  44.     * look for the hotkey, and call popup when you see it
  45.     */
  46.  
  47.    void interrupt far newint9(es,ds,di,si,bp,sp,bx,dx,cx,ax,
  48.                               ip,cs,flags)
  49.    unsigned es, ds, di, si, bp, sp, bx, dx, cx, ax, ip, cs, flags;
  50.    {
  51.          static unsigned char keycode;
  52.          static char active = 0;
  53.  
  54.          if (active)
  55.                _chain_intr(oldint9);
  56.  
  57.          keycode = inp(KEYPORT);
  58.          if (keycode != HOTKEY_CODE)
  59.                _chain_intr(oldint9);
  60.          if (_bios_keybrd(_KEYBRD_SHIFTSTATUS) & HOTKEY_STAT) {
  61.                /* got it */
  62.                active = 1;
  63.                (*oldint9)(); /* let normal routine process hotkey */
  64.                popup();
  65.                active =  0;
  66.          } else
  67.                _chain_intr(oldint9);
  68.    }
  69.  
  70.    main()
  71.    {
  72.          char huge *startofitall;
  73.          char huge *endofitall;
  74.          unsigned length;
  75.  
  76.          /* check dos ver */
  77.          if (_osmajor < 2) {
  78.                dos_puts("Not loaded, requires dos v2 or over.\r\n$");
  79.                exit();
  80.          }
  81.  
  82.          /* save old int9 */
  83.          oldint9 = _dos_getvect(KEYSVC_INT);
  84.          _dos_setvect(KEYSVC_INT, (void (interrupt far *)())newint9);
  85.  
  86.          /* some location work */
  87.          FP_SEG(startofitall) = _psp;
  88.          FP_OFF(startofitall) = 0;
  89.          endofitall = (char huge *)&end;
  90.          length = endofitall - startofitall; /* bytes */
  91.          if (length & 0xf) /* round up to next 16 byte para */
  92.                length += 0x10;
  93.          length >>= 4;     /* convert to paragraphs */
  94.  
  95.          dos_puts("Loaded ok\r\n$");
  96.          _dos_keep(0, length);   /* discards stack, keeps all else */
  97.    }
  98.  
  99.    /*
  100.     * print a $ terminated string
  101.     */
  102.    dos_puts(s)
  103.    char *s;
  104.    {
  105.          regs.x.dx = (int)s;
  106.          regs.h.ah = DOS_PRINTSTRING;
  107.          intdos(®s,®s);
  108.    }
  109.  
  110.    popup()
  111.    {
  112.          int oldcursor;
  113.  
  114.          /* swallow hotkey keystroke */
  115.          (void)_bios_keybrd(_KEYBRD_READ);
  116.  
  117.          /* save cursor */
  118.          oldcursor = *scr_get_curs_addr();
  119.  
  120.          /* put time into message */
  121.          bigben();
  122.  
  123.          /* popup message */
  124.          scr_swapmsg(msg, buf, 12, 30);
  125.  
  126.          /* wait for keystroke */
  127.          (void)_bios_keybrd(_KEYBRD_READ);
  128.  
  129.          /* erase message */
  130.          scr_swapmsg(buf, msg, 12, 30);
  131.  
  132.          /* restore cursor */
  133.          *scr_get_curs_addr() = oldcursor;
  134.    }
  135.  
  136.    /*
  137.     * Read ticks, convert to cumulative seconds, and then fill msg.
  138.     *  cum_secs = ticks/18.2065
  139.     *  (but I don't want to use floating point)
  140.     *  or, cum_secs = (ticks * 10,000) / 182065
  141.     *  (but that would overflow in a long)
  142.     *  or, cum_secs = (ticks * 2,000) / 36413
  143.     */
  144.  
  145.    bigben()
  146.    {
  147.          _bios_timeofday(_TIME_GETCLOCK, &ticks);
  148.          ticks *= 2000L;         /* convert ... */
  149.          ticks /= 36413L;  /* ... to seconds */
  150.          h = ticks / 3600;
  151.          m = ticks / 60;
  152.          m = m % 60;
  153.          s = ticks % 60;
  154.          sptr = msg + StartOfTime;
  155.          *sptr++ = h/10 + '0';
  156.          *sptr++ = h%10 + '0';
  157.          *sptr++ = ':';
  158.          *sptr++ = m/10 + '0';
  159.          *sptr++ = m%10 + '0';
  160.          *sptr++ = ':';
  161.          *sptr++ = s/10 + '0';
  162.          *sptr++ = s%10 + '0';
  163.    }
  164.